package com.ruoyi.gbase.task;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.gbase.domain.GbaseLoadCount;
import com.ruoyi.gbase.domain.TableInfo;
import com.ruoyi.gbase.service.IGbaseJobInfoService;
import com.ruoyi.gbase.service.ITableInfoService;
import com.ruoyi.gbase.util.SMSUtil;
import com.ruoyi.nsh.util.DateUtil;
import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.quartz.service.ISysJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * 定时每天晚上发送当入库和调度的进度  有问题及时处理
 * @author yys
 * 20211027
 */
@Component("sendSmsTask")
public class SendSmsTask
{
    //private static final Logger log = LoggerFactory.getLogger(SendSmsTask.class);

    /**
     * 发短信人员
     **/
    @Value("${sms.phoneNo}")
    private String phoneNo;


    @Autowired
    private IGbaseJobInfoService gbaseJobInfoService;
    @Autowired
    private ISysJobService jobService;

    @Autowired
    private ITableInfoService tableInfoService;
    /**
     * 手动任务_更新一次DB2数据的处理
     */
    public void sendSms()
    {
        String rq = DateUtil.addDay(DateUtils.getdateYYYYMMDD(),-1);
        GbaseLoadCount gbaseJobInfo = gbaseJobInfoService.selectGbaseLoadCount(rq);
        int xfnum = 0;
        int rknum = 0;

        AjaxResult ajax = AjaxResult.success();
        ajax.put("rq", rq);
        if(gbaseJobInfo!=null){
            xfnum = gbaseJobInfo.getDbnums();
            rknum = gbaseJobInfo.getGbasenums();
        }

        //存储过程结果
        SysJob job = new SysJob();
        job.setJobGroup("PROC");
        List<SysJob> list = jobService.selectJobList(job);
        job.setExecDt(DateUtil.addDay(DateUtils.getdateYYYYMMDD(),-0));
        job.setExecStatus("执行成功");
        List<SysJob> list2 = jobService.selectJobList(job);
        job.setExecDt(null);
        job.setExecStatus("执行失败");
        List<SysJob> listfail = jobService.selectJobList(job);
        job.setExecDt(null);
        job.setExecStatus("正在执行");
        List<SysJob> listing = jobService.selectJobList(job);

        SMSUtil.sendSms(phoneNo,rq+"下发:" + xfnum + " 已入库:"
                + rknum+" 存储过程："+list.size()+" 成功："+list2.size()
                +" 失败："+listfail.size()+" 正在执行：" +listing.size());
        this.tableChangeSendMsg();
    }


    /*
        检测昨天数据下发失败清空
     */
    public void tableChangeSendMsg(){
        TableInfo tableInfo = new TableInfo();
        tableInfo.setEtlDate(DateUtil.addDay(DateUtils.getdateYYYYMMDD(), -1));
        tableInfo.setTableChangeFlag("yes");
        List<TableInfo> tableInfoList = tableInfoService.selectTableInfoList(tableInfo);
        for(TableInfo t:tableInfoList){
            //执行发短信操作
            SMSUtil.sendSms(phoneNo,t.getSchemaName()+"."+t.getTableName()+" 表结构有变化了，请抓紧处理！！！");
        }

    }




}
